package com.wms.shop.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms.common.core.utils.DateUtils;
import com.wms.shop.domain.Cart;
import com.wms.shop.mapper.CartMapper;
import com.wms.shop.service.ICartService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wms.common.core.utils.SecurityUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 购物车Service业务层处理
 *
 * @author zzm
 * @date 2021-08-28
 */
@Service
public class CartServiceImpl implements ICartService
{
    @Autowired
    private CartMapper cartMapper;

    /**
     * 查询购物车
     *
     * @param id 购物车ID
     * @return 购物车
     */
    @Override
    public Cart selectCartById(Long id)
    {
        return cartMapper.selectById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param cart 购物车
     * @return 购物车
     */
    @Override
    public List<Cart> selectCartList(Cart cart)
    {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, cart.getUserId());
        queryWrapper.eq(Cart::getDelFlag, Boolean.FALSE);
        return cartMapper.selectList(queryWrapper);
    }

    /**
     * 新增购物车
     *
     * @param cart 购物车
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCart(Cart cart)
    {
        // 将其他购物车的内容置为未选中
        Cart form = new Cart();
        form.setSelected(Boolean.FALSE);
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getProductId, cart.getProductId());
        cartLambdaQueryWrapper.eq(Cart::getDelFlag, Boolean.FALSE);
        cartMapper.update(form, cartLambdaQueryWrapper);

        cart.setDelFlag(false);
        cart.setCreateBy(SecurityUtils.getUsername());
        cart.setCreateTime(DateUtils.getNowDate());
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Cart::getUserId, cart.getUserId());
        queryWrapper.eq(Cart::getSkuId, cart.getSkuId());
        queryWrapper.eq(Cart::getDelFlag, Boolean.FALSE);
        Cart pCard = cartMapper.selectOne(queryWrapper);
        if (pCard == null) {
            cart.setDelFlag(Boolean.FALSE);
            cart.setSelected(Boolean.TRUE);
            int result = cartMapper.insert(cart);
            return result;
        } else {
            pCard.setQuantity(pCard.getQuantity().add(cart.getQuantity()));
            pCard.setSelected(Boolean.TRUE);
            return this.updateCart(pCard);
        }

    }

    /**
     * 修改购物车
     *
     * @param cart 购物车
     * @return 结果
     */
    @Override
    public int updateCart(Cart cart)
    {
        cart.setUpdateBy(SecurityUtils.getUsername());
        cart.setUpdateTime(DateUtils.getNowDate());
        return cartMapper.updateById(cart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车ID
     * @return 结果
     */
    @Override
    public int deleteCartByIds(String[] ids)
    {
        Cart cart = new Cart();
        cart.setDelFlag(Boolean.TRUE);
        cart.setUpdateTime(DateUtils.getNowDate());
        cart.setUpdateBy(SecurityUtils.getUsername());
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",Arrays.asList(ids));
        return cartMapper.update(cart, queryWrapper);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车ID
     * @return 结果
     */
    @Override
    public int deleteCartById(Long id)
    {
        return cartMapper.deleteById(id);
    }

    /**
     * 根据用户计算购物车数量
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getCountByUserId(Long userId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        queryWrapper.eq(Cart::getDelFlag, 0);
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        BigDecimal qty = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(carts)) {
            for (Cart cart : carts) {
                qty = qty.add(cart.getQuantity());
            }
        }
        return qty;
    }

    @Override
    public Cart selectCartByProductId(Long productId, Long userId) {
        LambdaQueryWrapper<Cart> lambdaQueryWrapper =  new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Cart::getProductId, productId);
        lambdaQueryWrapper.eq(Cart::getUserId, userId);
        lambdaQueryWrapper.eq(Cart::getSelected, Boolean.TRUE);
        lambdaQueryWrapper.eq(Cart::getDelFlag, Boolean.TRUE);
        Cart cart = cartMapper.selectOne(lambdaQueryWrapper);
        return cart;
    }
}